package com.tdkj.action.backstage.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.tdkj.action.BaseAction;
import com.tdkj.model.user.BeanAdminLevel;
import com.tdkj.model.user.BeanPermission;
import com.tdkj.service.user.IBeanAdminLevelService;

public class AdminLevelAction extends BaseAction {
	private static final long serialVersionUID = -5638732883293595533L;
	private IBeanAdminLevelService adminLevelService;
	private long id;
	private String levelcode;
	private String levelName;
	private long modifyId;
	private String beanPermissionid;
	private BeanAdminLevel beanAdminLevel;
	private BeanPermission beanPermission;
	private List<BeanAdminLevel> levels = new ArrayList<BeanAdminLevel>();

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public List<BeanAdminLevel> getLevels() {
		return levels;
	}

	public void setLevels(List<BeanAdminLevel> levels) {
		this.levels = levels;
	}

	public String getLevelcode() {
		return levelcode;
	}

	public void setLevelcode(String levelcode) {
		this.levelcode = levelcode;
	}

	public String getLevelName() {
		return levelName;
	}

	public void setLevelName(String levelName) {
		this.levelName = levelName;
	}

	@Resource
	public void setAdminLevelService(IBeanAdminLevelService adminLevelService) {
		this.adminLevelService = adminLevelService;
	}

	// 获取级别列表
	public String findAll() {
		// appendObj();
		levels = adminLevelService.findAll(BeanAdminLevel.class);
		return "findAll";
	}

	// 添加级别
	public String addLevel() {
		// 验证级别名称是否存在
		if (adminLevelService.findByProperty("levelname", this.getLevelName(), BeanAdminLevel.class).size() > 0) {
			this.addFieldError("nameVlidate", "级别名称已经存在,请重新输入");
			return findAll();
		}
		BeanAdminLevel bean = new BeanAdminLevel();
		bean.setLevelname(this.getLevelName());
		BeanPermission beanPermission = new BeanPermission();
		adminLevelService.save(beanPermission);
		bean.setPermission(beanPermission);
		bean.setRemark("管理员添加");
		adminLevelService.save(bean);
		return findAll();
	}

	// 添加级别
	public String deleteLevel() {
		BeanAdminLevel bean = new BeanAdminLevel();
		long id = this.getId();
		bean.setId(this.getId());
		adminLevelService.delete(bean);
		return findAll();
	}

	private void appendObj() {
		// TODO Auto-generated method stub
		if (adminLevelService.findAll(BeanAdminLevel.class).size() < 10) {
			BeanAdminLevel bean = null;
			for (int i = 0; i < 10; i++) {
				bean = new BeanAdminLevel();
				bean.setLevelcode("code" + i);
				bean.setLevelname("用户等级" + i);
				BeanPermission beanPermission = new BeanPermission();
				adminLevelService.save(beanPermission);
				bean.setPermission(beanPermission);
				adminLevelService.save(bean);
			}
		}

	}

	// -------------------------权限设置--------------------------------------------

	/**
	 * 到权限分配页面
	 */
	public String modifyPermission() {
		beanAdminLevel = adminLevelService.findById(modifyId, BeanAdminLevel.class);
		if (null != beanAdminLevel) {
			/*
			 * if (null == beanAdminLevel.getPermission()) { BeanPermission
			 * beanPermissionTemp = new BeanPermission();
			 * adminLevelService.save(beanPermissionTemp);
			 * beanAdminLevel.setPermission(beanPermissionTemp); beanAdminLevel
			 * = adminLevelService.findById(modifyId, BeanAdminLevel.class);
			 * beanPermission = beanAdminLevel.getPermission(); } else {
			 * beanPermission = beanAdminLevel.getPermission(); }
			 */
			beanPermission = beanAdminLevel.getPermission();
			beanPermissionid = beanPermission.getId() + "";
			return "admin_level_permission";
		}
		return findAll();
	}

	/**
	 * 保存权限分配结果
	 * 
	 * @return
	 */
	public String savePermission() {
		beanPermission.setId(Long.parseLong(beanPermissionid));
		adminLevelService.saveOrUpdate(beanPermission);
		return findAll();
	}

	public long getModifyId() {
		return modifyId;
	}

	public void setModifyId(long modifyId) {
		this.modifyId = modifyId;
	}

	public IBeanAdminLevelService getAdminLevelService() {
		return adminLevelService;
	}

	public BeanAdminLevel getBeanAdminLevel() {
		return beanAdminLevel;
	}

	public void setBeanAdminLevel(BeanAdminLevel beanAdminLevel) {
		this.beanAdminLevel = beanAdminLevel;
	}

	public BeanPermission getBeanPermission() {
		return beanPermission;
	}

	public void setBeanPermission(BeanPermission beanPermission) {
		this.beanPermission = beanPermission;
	}

	public String getBeanPermissionid() {
		return beanPermissionid;
	}

	public void setBeanPermissionid(String beanPermissionid) {
		this.beanPermissionid = beanPermissionid;
	}

}
